// pages/DiseaseDefend/DDList/DDList.js
const db = wx.cloud.database();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    searchkey: '',
    regx: false, // true 为模糊搜索
  },

  /**
   * 组件的方法列表
   */
  lifetimes: {
    created() {
      var that = this;
      that.reqData();
    }
  },
  methods: {
    GetSearchKey(e){
      var that = this;
      //console.log(e)
      that.setData({
        searchkey:e.detail.value
      })
    },
    toSearchMx(){
      var that = this;
      that.setData({
        regx:true,
        list:[]
      },()=>{
        that.reqData();
      })
    },
    toSearch() {
      var that = this;
      that.setData({
        regx: false,
        list: []
      }, () => {
        that.reqData();
      })
    },
    Call(e) {
      var _ = e.currentTarget.dataset.phone;
      wx.showModal({
        title: '是否呼叫?',
        content: '',
        success(_res) {
          if (_res.confirm) {
            wx.makePhoneCall({
              phoneNumber: _,
            })
          }
        }
      })
    },
    reqData() {
      var that = this;
      var _ = that.data;
      wx.showLoading();
      if (that.data.searchkey == '') {
        //console.log(_.list.length)
        db.collection('authlog').skip(_.list.length).limit(10).get({
          success(res) {
            //console.log(res)
            wx.hideLoading();
            that.setData({
              list: _.list.concat(res.data)
            })
          },
          fail(res) { //console.log(res);
            wx.hideLoading();
            wx.showToast({
              title: '请求错误',
            })
          }
        })
      } else {
        if (_.regx) {
          console.log('模糊搜索')
          console.log(_.searchkey)
          //模糊搜索
          db.collection('authlog').skip(_.list.length).limit(10).where(db.command.or([
            {
              name:  db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              phone:  db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              traffic_id: db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              fromprovince: db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              idcard: db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              fromregion: db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              fromcity: db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              destinprovince: db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              destinregion: db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              destincity: db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
            {
              traffic_type: db.RegExp({
                regexp: _.searchkey,
                options: 'm',
              })
            },
          ])).get({
            success(res){
              console.log(res)
              wx.hideLoading();
              that.setData({
                list: _.list.concat(res.data)
              })
            },fail(res){
              wx.hideLoading();
              wx.showToast({
                title: '请求fail',
              })
            }
          })
        } else {
          //精确搜索
          db.collection('authlog').skip(_.list.length).limit(10).where(db.command.or([
            {
              name:  _.searchkey,
            },
            {
              phone: _.searchkey
            },
            {
              traffic_id: _.searchkey
            },
            {
              fromprovince: _.searchkey
            },
            {
              idcard: _.searchkey
            },
            {
              fromregion: _.searchkey
            },
            {
              fromcity: _.searchkey
            },
            {
              destinprovince: _.searchkey
            },
            {
              destinregion: _.searchkey 
            },
            {
              destincity: _.searchkey
            },
            {
              traffic_type: _.searchkey
            },
          ])).get({
            success(res) {
              console.log(res)
              wx.hideLoading();
              that.setData({
                list: _.list.concat(res.data)
              })
            }, fail(res) {
              wx.hideLoading();
              wx.showToast({
                title: '请求fail',
              })
            }
          })
        }
      }
    },
  }
})